<?php
/**
 * This class is intended to make Person request's to the container. Person request's are classes that extends the abstract class OSPerson naming OSViewer or OSOwner, which allows to get the viewer and the owner respectively.
 * Basically to use this class you need instantiate a class that extends the abstract class OSPerson:
 * <code> $this->viewer = new OSViewer('viewer');
 * //or
 * $this->owner = new OSOwner('owner');
 * </code>
 * Then you need to create an OSPersonRequest instance, passing has argument one of the variables previously created:
 * <code>
 * $this->viewer_request = new OSPersonRequest($this->viewer);
 * //or
 * $this->owner_request = new OSPersonRequest($this->owner);
 * </code>
 * Now that you have created the OSPersonRequest object you need to create an OSDataRequest and add that created object to it:
 * <code>
 * $data_request = new OSDataRequest('requestPerson');
 * $data_request->addRequest($this->viewer_request);
 * //or
 * $data_request->addRequest($this->owner_request);
 * </code>
 * After adding the OSPersonRequest to the OSDataRequest you need to call the function "send()" so that this OSDataRequest will be made to the container:
 * <code>
 * $this->data_response = $data_request->send(true);
 * </code>
 * It has been created the request to the container and now you can access to OSViewer or OSOwner from the view and iterate over those objects in the view:
 * <code>
 * // View
 * <script type="text/javascript">
 * function <? echo $data_response->getJSFunction()->getName() ?>(<? echo $data_response->getJSFunction()->getArgs() ?>) 
 * {
 *   var html = '';
 *   <?php echo $data_response->getData($owner_request); ?>
 *   var owner_name = <?php echo $owner->getName(); ?>
 *   var owner_thumb = <?php echo $owner->getField(OSPersonField::THUMBNAIL_URL); ?>
 *   var owner_profile = <?php echo $owner->getField(OSPersonField::PROFILE_URL); ?>
 *   var owner_nick = <?php echo $owner->getField(OSPersonField::NICKNAME); ?>
 *   html = '<a href="' + owner_profile + '" target="_top" style="float:left">' +'<img src="' + owner_thumb + '"/></a>' +
 *     owner_name + ' - '+owner_nick+'</a>'; 
 *   document.getElementById("owner").innerHTML = html;
 * 
 *   <?php echo $data_response->getData($viewer_request); ?>
 *   var viewer_name = <?php echo $viewer->getName(); ?>
 *   var viewer_thumb = <?php echo $viewer->getField(OSPersonField::THUMBNAIL_URL); ?>
 *   var viewer_profile = <?php echo $viewer->getField(OSPersonField::PROFILE_URL); ?>
 *   var viewer_nick = <?php echo $viewer->getField(OSPersonField::NICKNAME); ?>
 *   html = '<a href="' + viewer_profile + '" target="_top" style="float:left">' +'<img src="' + viewer_thumb + '"/></a>' +
 *     viewer_name + ' - '+viewer_nick+'</a>'; 
 *   document.getElementById("viewer").innerHTML = html
 *   <?php echo GadgetsWindow::adjustHeight(); ?>
 *   
 * }
 * </script></code>
 * 
 * @package    opensocial
 * @subpackage datarequest
 * @author     Daniel Botelho <botelho.daniel@gmail.com>
 */
class OSPersonRequest implements OSDataRequestable
{ 
  // {1} - Friends Type (VIEWER_FRIENDS/OWNER_FRIENDS)
  // {2} - Response variable for this request( 
  private static $NEWFETCHPERSONREQUEST_PATTERN = "newFetchPersonRequest(%s,%s),\"%s\"";

  // {1} - nome da variavel esta a guardar os parametros
  // {2} - PeopleRequestFields
  // {3} - valor a definir
  private static $PERSONREQUESTPARAMS_PATTERN = "%s[%s] = %s;\n";

  //corresponde a um objecto: Friends
  private $person;

  // Nome da variável usada para os parametros deste PeopleRequest
  private $personRequestParams_var;

  // Um array com os parametros
  // exemplo: opt_params[opensocial.DataRequest.PeopleRequestFields.PROFILE_DETAILS] = [opensocial.Person.Field.PROFILE_URL];
  private $personRequestParams = array();

  private $profileDetails_var;
  private /*OSPersonField[]*/ $profileDetails = array();


  /**
  * OSPersonRequest constructor
  * @param OSPerson The OSPerson object
  */
  function __construct(OSPerson $person)
  {
    $this->person = $person;
    $this->personRequestParams_var = $person->getVarName().'_personRequestParams';
    $this->profileDetails_var =  $person->getVarName().'_profileDetailsRequestParams';
  }
  
  public function getPersonRequestParamsVarName() { return $this->personRequestParams_var; }
  
  /**
   * Get's the OSPerson Object
   * @return OSPerson The object associated to this OSPersonRequest.
   */
  public function getPerson()
  {
    return $this->person;
  }
  
  /**
   * Gets the all the params requested
   * @return array The Request params.
   */
  public function getPersonRequestParams()
  {
    return $this->personRequestParams;
  }
  
  /**
   * This method returns the var name for profileDetails
   * @return string Profile details var name.
   */
  public function getProfileDetailsVarName()
  {
    return $this->profileDetails_var;
  }
  
  /**
   * Gets the profile details
   * @return array The profile details.
   */
  public function getProfileDetails()
  {
    return $this->profileDetails;
  }

  public function generateDataRequest(OSDataRequest $dataRequest)
  {
  	//vai buscar o nome da variável que é usada para o dataRequest
    $request_var = $dataRequest->getDataRequestVarName();

    // vai buscar a informação referente ao profile
    $html = 'var '.$this->getProfileDetailsVarName()." = new Array();\n";

    foreach($this->getProfileDetails() as $profileDetail) {
      $html .= $this->getProfileDetailsVarName().".push($profileDetail);\n";
    }

    $params_var = $this->getPersonRequestParamsVarName();
    $params = $this->getPersonRequestParams();

    $html .= 'var '.$params_var." = { };\n";

    foreach($params as $key => $value) {
      $html .= sprintf(OSPersonRequest::$PERSONREQUESTPARAMS_PATTERN,$params_var,$key,$value);
    }
    $html .= $request_var.".add(".$request_var.".".sprintf(OSPersonRequest::$NEWFETCHPERSONREQUEST_PATTERN,$this->getPerson()->getType(),$params_var,$this->getPerson()->getVarName()).");\n";
  	return $html;
  }
  
  public function getOptionalKey()
  {
  	return $this->getPerson()->getVarName();
  }
  /**
   * Set's the sort order. Look at OSDataRequestSortOrder to see the available params.
   * @see OSDataRequestSortOrder
   */
  public function setSortOrder(/*OSDataRequestSortOrder*/ $sortOrder)
  {
    $this->personRequestParams[OSDataRequestPeopleRequestFields::SORT_ORDER] = "[$sortOrder]";
  }
  /**
   * Set's the filter type. Look at OSDataRequestFilterType to see the available params.
   * @see OSDataRequestFilterType
   */
  public function setFilterType(/*OSDataRequestFilterType*/ $filterType)
  {
    $this->personRequestParams[OSDataRequestPeopleRequestFields::FILTER] = "[$filterType]";
  }
  /**
   * Set's the index of the first item to fetch.
   * @param int $first The index of the first item to fetch.
   * @link http://code.google.com/apis/opensocial/docs/0.7/reference/opensocial.DataRequest.PeopleRequestFields.html#FIRST
   */
  public function setFirst(/*Number*/ $first)
  {
    $this->personRequestParams[OSDataRequestPeopleRequestFields::FIRST] = "[$first]";
  }
  /**
   * Set's the maximum number of items to fetch; 
   * defaults to 20. If set to a larger number, a container may honor the request, or may limit the number to a container-specified limit of at least 20
   * 
   * @param int $max The maximum number of items to fetch.
   * @link http://code.google.com/apis/opensocial/docs/0.7/reference/opensocial.DataRequest.PeopleRequestFields.html#MAX
   */
  public function setMax(/*OSDataRequestFilterType*/ $max)
  {
    $this->personRequestParams[OSDataRequestPeopleRequestFields::MAX] = "[$max]";
  }
  /**
   * Set's the profile details array.
   * @param array One array containing the profile details.   
   */  
  public function setProfileDetails(/*OSPersonField*/ $profileDetails)
  {
    if(count($profileDetails))
      $this->personRequestParams[OSDataRequestPeopleRequestFields::PROFILE_DETAILS] = $this->profileDetails_var;
    $this->profileDetails = $profileDetails;
  }
  /**
   * Add's an request for profile detail. Look at OSPersonField to see the available params.
   * @see OSPersonField
   */
  public function addProfileDetails(/*OSPersonField*/ $profileDetail)
  {
    if(count($this->profileDetails) == 0 )
      $this->personRequestParams[OSDataRequestPeopleRequestFields::PROFILE_DETAILS] = $this->profileDetails_var;
    $this->profileDetails[] = $profileDetail;
  }
  
}
